﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_DiemDanhController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;

        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }
      
        public ActionResult Index(ViewModelDKH_DD SearchModel)
        {
            initialCategorySearchAction(SearchModel);
            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_DIEMDANH_HOCVIEN(SearchModel.pDVGTVT_MA
                                                                                               , SearchModel.pKH_MA
                                                                                               , SearchModel.pHV_MA
                                                                                               , SearchModel.pHV_HOTEN
                                                                                               , SearchModel.pHV_CMND
                                                                                               , SearchModel.pHV_GIAIDOAN_MA
                                                                                               , SearchModel.pHV_NGAY_DD_TU
                                                                                               , SearchModel.pHV_NGAY_DD_DEN
                                                                                              );

        
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            ViewBag.TotalRow = entities.Count();
            return View(SearchModel);
        }

        public void initialCategorySearchAction(ViewModelDKH_DD viewModel)
        {
            string ma_DVGT = this.getCurrentUser().MA_TINHTP;

            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pDVGTVT_MA);
            viewModel.pDVGTVT_MA = getdvgtvt_Ma;

            string MaKhoaHoc = null;
            GPS.DAL.Check_User.Substring_Autocomplate(viewModel.pKH_MA, ref MaKhoaHoc);
            viewModel.pKH_MA = MaKhoaHoc;

            //ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            //ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            ////KH_MAs
            //var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            //ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MA == ma_DVGT), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == ma_DVGT);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");


        }


        //public ActionResult Create()
        //{
        //    initialCategoryCreateAction();
        //    CrudModelDKH_LOP_DD model = new CrudModelDKH_LOP_DD();
        //    //var hvs = _db.GetAll<DHV>().Take(20).ToList();
        //    model.ListHVs = new List<SelectItemDHV>();
        //    //hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
        //    return View(model);
        //}

        //[HttpPost]
        //public ActionResult Create(CrudModelDKH_LOP_DD model)
        //{
        //    if (ModelState.IsValid)
        //    {

                
              
        //            if (model.SelectedHVs != null && model.SelectedHVs.Length > 0)
        //            {
                       
        //                for (int i = 0; i < model.SelectedHVs.Length; i++)
        //                {
        //                    DKH_LOP_DD _model = new DKH_LOP_DD();
        //                    //SEQ_DKH_LOP_DD_MA
        //                    var _KH_LOP_DD_MA = EFSequence.GetNextValue("SEQ_DKH_LOP_DD_MA");

        //                    GPS.COMMON.Helpers.CopyObject<DKH_LOP_DD>(model, ref _model, new List<string> { "SelectedHVs", "SelectItemDHV" });
        //                    _model.KH_LOP_DD_HV_MA = model.SelectedHVs[i];
        //                    _model.KH_LOP_DD_MA = _KH_LOP_DD_MA.ToString();
        //                    _db.Insert<DKH_LOP_DD>(_model);
        //                }

        //            }
                

        //        return RedirectToAction("Index");
        //    }
        //    initialCategoryCreateAction();
        //    model.ListHVs = new List<SelectItemDHV>();
        //    return View(model);
        //}

        //public ActionResult Edit(string id1)
        //{
        //    string _KH_LOP_DD_MA = id1.ToString();

        //    DKH_LOP_DD model = _db.GetOne<DKH_LOP_DD>(o => o.KH_LOP_DD_MA == id1);

        //    CrudModelDKH_LOP_DD _model = new CrudModelDKH_LOP_DD();
        //    GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP_DD>(model, ref _model, new List<string> { "SelectedHVs", "SelectItemDHV" });

        //    DHV _hv = _db.GetOne<DHV>(o => o.HV_MA == model.KH_LOP_DD_HV_MA && o.HV_DVGTVT_MA == model.KH_LOP_DD_DVGTVT_MA);
        //    ViewBag.HANGHT = _hv.HV_HANGDT_MA;
        //    ViewBag.HV_TEN = _hv.HV_TEN;

        //    initialCategoryEditAction(_model);
        //    return View(_model);
        //}

        //[HttpPost]
        //public ActionResult Edit(CrudModelDKH_LOP_DD model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var _model = _db.GetOne<DKH_LOP_DD>(o => o.KH_LOP_DD_MA == model.KH_LOP_DD_MA );
        //        GPS.COMMON.Helpers.CopyObject<DKH_LOP_DD>(model, ref _model, new List<string> { "KH_LOP_LH_ACTIVE", "SelectedHVs", "SelectItemDHV" });

        //        _db.Update<DKH_LOP_DD>(_model);

                

        //        return RedirectToAction("Index");
        //    }
        //    initialCategoryEditAction(model);
        //    return View(model);
        //}

        //public ActionResult Delete(string id1)
        //{
        //    string _KH_LOP_DD_MA = id1.ToString();

        //    _db.Delete<DKH_LOP_DD>(o => o.KH_LOP_DD_MA == id1);

        //    return RedirectToAction("Index");
        //}

        //[HttpPost]
        //public ActionResult Delete(CrudModelDKH_LOP_DD model)
        //{
        //    return View();
        //}

        //#region detail
        //public ActionResult Detail(string id)
        //{

        //    DKH_LOP_DD model = _db.GetOne<DKH_LOP_DD>(o => o.KH_LOP_DD_MA == id);

        //    CrudModelDKH_LOP_DD _model = new CrudModelDKH_LOP_DD();
        //    GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP_DD>(model, ref _model);

        //    initialCategoryDetailAction(_model);
        //    return PartialView("_Detail", _model);
        //}
        //public void initialCategoryDetailAction(CrudModelDKH_LOP_DD view)
        //{
        //    ViewBag.dvgtvt = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == view.KH_LOP_DD_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
        //    ViewBag.khoahocs = _db.Filter<DKH>(o => o.KH_MA == view.KH_LOP_DD_KH_MA).Select(x => x.KH_TEN).FirstOrDefault();
        //    ViewBag.lophocs = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == view.KH_LOP_DD_KH_LOP_MA).Select(x => x.KH_LOP_TEN).FirstOrDefault();

        //    ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
           
        //    ViewBag.PT_MAs = CategoryCommon.DEFAULT_VALUE_DDL;
        //    //if (view.KH_LOP_LH_HTDT_MA != null && view.KH_LOP_LH_HTDT_MA != "LT")
        //    //{
        //    //    ViewBag.PT_MAs = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == view.KH_LOP_LH_DVGTVT_MA).Select(x => x.PT_BIENSO).FirstOrDefault();
        //    //}

          

        //}

        //#endregion

        //#region"Ajax"
        //[HttpPost]
        //public ActionResult getKH_MAs(string ma_DVGT)
        //{
        //    var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_MA + "</option>");

        //    return Content(string.Join("", districts));
        //}

        //[HttpPost]
        //public ActionResult getKH_LOP_MAs(string ma_KH)
        //{
        //    var districts = _db.Filter<DKH_LOP>(d => d.KH_LOP_KH_MA == ma_KH).Select(a => "<option value='" + a.KH_LOP_MA + "'>" + a.KH_LOP_MA + "</option>");

        //    return Content(string.Join("", districts));
        //}

        //[HttpPost]
        //public ActionResult getGV_MAs(string Ma_DVGT, string ma_LOP)
        //{
        //    var districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

        //    if (ma_LOP == null || string.IsNullOrEmpty(ma_LOP))
        //    {

        //        districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

        //    }
        //    else
        //    {
        //        var MA_DGV = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP).KH_LOP_GV_MA;
        //        districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT && d.GV_MA == MA_DGV).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

        //    }

        //    return Content(string.Join("", districts));
        //}

        //[HttpPost]
        //public ActionResult getPT_MAs(string Ma_DVGT, string ma_HT, string ma_LOP)
        //{
        //    var MA_DPT = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP).KH_LOP_PT_MA;
        //    var districts = _db.Filter<DPT>(d => d.PT_DVGTVT_MA == Ma_DVGT && d.PT_BIENSO == MA_DPT).Select(a => "<option value='" + a.PT_BIENSO + "'>" + a.PT_BIENSO + "</option>");

        //    return Content(string.Join("", districts));
        //}

        ////  getHANGDTGP_MAs
        //[HttpPost]
        //public JsonResult getHANGDTGP_MAs(string ma_LOP)
        //{

        //    var str1 = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP);
        //    var str = str1.KH_LOP_HANGDT_MA + "," + str1.KH_LOP_HGPLX_MA;
        //    return Json(str);
        //}


        ////get Học viên
        //[HttpPost]
        //public ActionResult getHV_MAs(string Ma_DVGT, string ma_KH, string ma_LOP)
        //{
        //    List<ModelDHV> _lst = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GetdataHocVienInLop(Ma_DVGT, ma_KH, ma_LOP);
        //    var districts = _lst.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HODEM + " " + a.HV_TEN + "</option>"); ;

        //    return Content(string.Join("", districts));
        //}

        //#endregion

        //#region Function



        ////private bool IsCollidesWithOthers(CrudModelDKH_LOP_DD _model)
        ////{
        ////    if (_model != null)
        ////    {
        ////        var collidedBookings = _db.EntityExists<DKH_LOP_DD>(
        ////        b => b.KH_LOP_LH_MA != _model.KH_LOP_LH_MA.ToString() && (b.KH_LOP_LH_GV_MA == _model.KH_LOP_LH_GV_MA || b.KH_LOP_LH_PT_BIENSO == _model.KH_LOP_LH_PT_BIENSO)
        ////            && b.KH_LOP_LH_TUNGAY < _model.KH_LOP_LH_TUNGAY && b.KH_LOP_LH_DENNGAY > _model.KH_LOP_LH_DENNGAY
        ////        );

        ////        return collidedBookings;
        ////    }

        ////    return false;
        ////}

       
        //public void initialCategoryCreateAction()
        //{
        //    // ApplicationUser curUser = getCurrentUser();

        //    ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
          
        //}

        //public void initialCategoryEditAction(CrudModelDKH_LOP_DD view)
        //{
        //    DKH_LOP _lop = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == view.KH_LOP_DD_KH_LOP_MA);
        //    ViewBag.HANGHT = _lop.KH_LOP_HANGDT_MA;
        //    ViewBag.HANGGPLX = _lop.KH_LOP_HGPLX_MA;
        //    var _magv = _lop.KH_LOP_GV_MA;
        //    var _bienso = _lop.KH_LOP_PT_MA;

        //    // ApplicationUser curUser = getCurrentUser();
        //    ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
          
        //    //KH_MAs
        //    var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == view.KH_LOP_DD_DVGTVT_MA);
        //    ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
        //    //KH_LOP_MAs
        //    var lophocs = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == view.KH_LOP_DD_KH_MA);
        //    ViewBag.KH_LOP_MAs = new SelectList(lophocs, "KH_LOP_MA", "KH_LOP_MA");

           

        //}
        //#endregion
    }
}